public class ComplexNumber {
	private double m_dRealPart, m_dImaginPart;

	public ComplexNumber() {
		m_dRealPart = m_dImaginPart = 0;
	}
	
	public ComplexNumber(double m_dRealPart, double m_dImaginPart) {
		this.m_dRealPart = m_dRealPart;
		this.m_dImaginPart = m_dImaginPart;
	}
	
	public double GetRealPart() {
		return m_dRealPart;
	}
	
	public double GetImaginaryPart() {
		return m_dImaginPart;
	}
	
	public void SetRealPart(double d) {
		this.m_dRealPart = d;
	}
	
	public void SetImaginaryPart(double d) {
		this.m_dImaginPart = d;
	}
	
	public ComplexNumber ComplexAdd(ComplexNumber c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dRealPart = this.m_dRealPart + c.m_dRealPart;
		p.m_dImaginPart = this.m_dImaginPart + c.m_dImaginPart;
		return p;
	}
	 
	public ComplexNumber ComplexAdd(double c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dRealPart = this.m_dRealPart + c;
		p.m_dImaginPart = this.m_dImaginPart;
		return p;
	}
	
	public ComplexNumber ComplexMinus(ComplexNumber c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dRealPart = this.m_dRealPart - c.m_dRealPart;
		p.m_dImaginPart = this.m_dImaginPart - c.m_dImaginPart;
		return p;
	}
	
	public ComplexNumber ComplexMinus(double c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dRealPart = this.m_dRealPart - c;
		p.m_dImaginPart = this.m_dImaginPart;
		return p;
	}
	
	public ComplexNumber ComplexMulti(ComplexNumber c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dImaginPart = this.m_dImaginPart * c.m_dRealPart + this.m_dRealPart * c.m_dImaginPart;
		p.m_dRealPart = this.m_dRealPart * c.m_dRealPart - this.m_dImaginPart * c.m_dImaginPart;
		return p;
	}
	
	public ComplexNumber ComplexMulti(double c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dRealPart = this.m_dRealPart * c;
		p.m_dImaginPart = this.m_dImaginPart * c;
		return p;
	}
	
	public ComplexNumber ComplexDiv(ComplexNumber c) {
		ComplexNumber p = new ComplexNumber(c.m_dRealPart, -c.m_dImaginPart);
		double t = c.m_dRealPart*c.m_dRealPart + c.m_dImaginPart*c.m_dImaginPart;
		p = p.ComplexMulti(this);
		p = p.ComplexDiv(t);
		return p;
	}
	
	public ComplexNumber ComplexDiv(double c) {
		ComplexNumber p = new ComplexNumber();
		p.m_dRealPart = this.m_dRealPart / c;
		p.m_dImaginPart = this.m_dImaginPart / c;
		return p;
	}
	
	public String toString() {
		String s = "";
        s = Double.toString(this.m_dRealPart);
        if (this.m_dImaginPart == 0) return s;
        if (this.m_dImaginPart > 0) s += '+';
        s += Double.toString(this.m_dImaginPart) + "i";

		return s;
	}
	
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof ComplexNumber)) return false;
		
		ComplexNumber obj = (ComplexNumber) o;
		if (fabs(this.m_dImaginPart - obj.m_dImaginPart) > 0.000001) return false;
		if (fabs(this.m_dRealPart - obj.m_dRealPart) > 0.000001) return false;
		return true;
	}

	private double fabs(double d) {
		if (d < 0) d = -d;
		return d;
	}
	
}
